#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
//тесты на mccme (с восстановлением пути и длиной) прошло
const int plusINF = INT_MAX;
const int minusINF = (-1)*INT_MAX;

std::vector<int> inputNumbers(){
    size_t size;
    std::cin >> size;
    std::vector<int> data;
    data.resize(size);
    for (int i = 0; i < size; ++i)
        std::cin >> data[i];
    return data;
}

int main()
{
    
    auto data = inputNumbers();
    std::vector<int> minEnds, posOfMinEnds, ancestors, result;
    minEnds.assign(data.size()+1, plusINF);
    posOfMinEnds.resize(minEnds.size());
    posOfMinEnds[0] = -1;
    ancestors.resize(minEnds.size());
    minEnds[0] = minusINF;
    int maxLength = 0;
    
    for (size_t i = 0; i < data.size(); ++i)
    {
        auto next = std::lower_bound(minEnds.begin(), minEnds.end(), data[i]);
        if (next != minEnds.end()){
            *next = data[i];
            posOfMinEnds[next-minEnds.begin()] = (int)i;
            ancestors[i] = posOfMinEnds[next-minEnds.begin()-1];
            maxLength = std::max(maxLength, (int)(next-minEnds.begin()));
        }
    }

    int i = posOfMinEnds[maxLength];
    while(i != -1)
    {
        result.push_back(data[i]);
        i = ancestors[i];
    }
    for (int i = (int)result.size()-1; i >= 0; i--)
        std::cout << result[i] << " ";

    return 0;
}
